package com.neo.job;

import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.stereotype.Component;

import com.neo.model.User;
import com.neo.rabbit.direct.DirectSender;
import com.neo.rabbit.fanout.FanoutSender;
import com.neo.rabbit.many.NeoSender;
import com.neo.rabbit.many.NeoSender2;
import com.neo.rabbit.object.ObjectSender;
import com.neo.rabbit.simple.HelloSender;
import com.neo.rabbit.topic.TopicSender;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * ScheduleJob
 * 
 * @author 00fly
 * @version [版本号, 2022年11月30日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Slf4j
@Component
@EnableScheduling
public class ScheduleJob
{
    @Autowired
    DirectSender directSender;
    
    @Autowired
    FanoutSender fanoutSender;
    
    @Autowired
    HelloSender helloSender;
    
    @Autowired
    NeoSender neoSender;
    
    @Autowired
    NeoSender2 neoSender2;
    
    @Autowired
    ObjectSender objectSender;
    
    @Autowired
    TopicSender topicSender;
    
    /**
     * 每10秒执行<br>
     * 
     */
    @Scheduled(cron = "0/10 * * * * ?")
    public void run1()
    {
        int num = new Random().nextInt(8);
        log.info("current: {}", num);
        switch (num)
        {
            case 1:
                directSender.send();
                break;
            case 2:
                fanoutSender.send();
                break;
            case 3:
                helloSender.send();
                break;
            case 4:
                for (int i = 0; i < 10; i++)
                {
                    neoSender.send(i);
                }
                break;
            case 5:
                for (int i = 0; i < 10; i++)
                {
                    neoSender.send(i);
                    neoSender2.send(i);
                }
                break;
            case 6:
                User user = new User();
                user.setName("neo");
                user.setPass("123456");
                objectSender.send(user);
                break;
            case 7:
                topicSender.send();
                topicSender.send1();
                topicSender.send2();
                break;
            default:
                break;
        }
        
    }
    
    @Bean
    public ScheduledExecutorService scheduledExecutorService()
    {
        // return Executors.newScheduledThreadPool(10);
        return new ScheduledThreadPoolExecutor(10, new CustomizableThreadFactory("schedule-pool-"));
    }
    
}
